home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / DA / P / PCalculator.cpt / CalcDA sources / CalcDA_ASCII.c < prev    next >
Text File  |  1990-07-06  |  5KB  |  176 lines

  1. /*
  2.  * CalcDA_ASCII.c for the Programmer's Calculator project 
  3.  * 
  4.  * Copyright 1990, Peter Ohler 
  5.  * 
  6.  * All Rights Reserved 
  7.  * 
  8.  * The Programmer's Calculator and the source code are shareware.  That means
  9.  * they are not free.  If you use either the source or the calculator then
  10.  * send $5 or $10 (whatever you feel its worth) to the address that follows.
  11.  * The source and calculator can be distributed for free.  Prior to any sale
  12.  * of either the source code or the calculator my permission must be
  13.  * obtained.  This includes sales by shareware distribution houses that sell
  14.  * shareware.  
  15.  * 
  16.  * Peter Ohler 
  17.  * 
  18.  * 3184 Rohrer Drive, Lafayette CA 94549 
  19.  * 
  20.  * (415) 284-7828 
  21.  * 
  22.  * ***************************************************************************
  23.  * 
  24.  * I can never remember all the ASCII characters used on the MAC and can only
  25.  * remember a few of the hex values for the character set.  The ASCII window
  26.  * displays the ASCII character in an array of the high and low bytes for
  27.  * each character.  
  28.  */
  29. #include <DeviceMgr.h>
  30. #include <WindowMgr.h>
  31. #include <pascal.h>
  32.  
  33. #include "CalcDA.h"
  34.  
  35. /*
  36.  * ***************************************************************************
  37.  * #defines
  38.  */
  39. #define ASCII_WIDTH    318
  40. #define ASCII_HIEGHT    270
  41.  
  42. #define ASCII_V_SPC    15
  43. #define ASCII_H_SPC    18
  44. #define ASCII_V_EDGE    36
  45. #define ASCII_H_EDGE    30
  46.  
  47. /*
  48.  * ***************************************************************************
  49.  * prototypes 
  50.  */
  51. extern void    CenterDrawPStr(char *str, int left, int right, int bottom);
  52. extern void    MoveDrawPStr(char *str, int h, int v);
  53.  
  54. int    DoAscii(void);
  55. void    UpdateAscii(void);
  56.  
  57. /*
  58.  * ***************************************************************************
  59.  * variables 
  60.  */
  61. extern DCtlPtr     deviceControlEntry;    /*  device control entry      */
  62.  
  63. WindowPtr    AsciiWindow = 0L;
  64.  
  65. /* The first 32 non-printing characters. */
  66. char    lowAscii[32][4] = { "\pNUL", "\pSOH", "\pSTX", "\pETX", "\pEOT", "\pENQ",
  67.                 "\pACK", "\pBEL", "\pBS",  "\pHT",  "\pLF",  "\pVT",
  68.                 "\pFF",  "\pCR",  "\pSO",  "\pSI",  "\pDLE", "\pDC1",
  69.                 "\pDC2", "\pDC3", "\pDC4", "\pNAK", "\pSYN", "\pETB",
  70.                 "\pCAN", "\pEM",  "\pSUB", "\pESC", "\pFS",  "\pGS",
  71.                 "\pRS",  "\pUS"
  72.               };
  73.  
  74. /*
  75.  * ***************************************************************************
  76.  * functions 
  77.  */
  78. int
  79. DoAscii()
  80. {
  81.     GrafPtr    savePort;
  82.     Rect    bounds;
  83.     Point    pt;
  84.  
  85.     GetPort(&savePort);
  86.     if (AsciiWindow) {
  87.         DisposeWindow(AsciiWindow);
  88.         AsciiWindow = 0L;
  89.     } else {
  90.         SetPort(CalcWindow);
  91.         SetPt(&pt, CalcWindow->portRect.right, CalcWindow->portRect.top);
  92.         LocalToGlobal(&pt);
  93.         pt.h += 6;
  94.         pt.v += 20;
  95.         SetRect(&bounds, pt.h, pt.v, pt.h + ASCII_WIDTH,
  96.             pt.v + ASCII_HIEGHT);
  97.         /*
  98.          * No close box.  If one is allowed then closing the ASCII
  99.          * window will cause the DA to get a goodbye when it is
  100.          * closed.  To avoid this behavior we use a button on the
  101.          * calculator to close the ASCII window.  
  102.          */
  103.         if (0L == (AsciiWindow = NewWindow(0L, &bounds, "\pASCII Chart",
  104.                            TRUE, 18, -1L, FALSE, 0L)))
  105.             return -1;
  106.         /* IMPORTANT - set the windowkind to the dCtlRefNum */
  107.         ((WindowPeek)AsciiWindow)->windowKind = deviceControlEntry->dCtlRefNum;
  108.     }
  109.     SetPort(savePort);
  110.     return 0;
  111. }
  112.  
  113. void
  114. UpdateAscii()
  115. {
  116.     int    h, v, i, ch;
  117.     char    *c;
  118.     Rect    bounds = { ASCII_V_EDGE - 12,
  119.                ASCII_H_EDGE - ASCII_H_SPC / 2 + 3,
  120.                ASCII_V_EDGE + 16 * ASCII_V_SPC - 12,
  121.                ASCII_H_EDGE + 16 * ASCII_H_SPC - ASCII_H_SPC / 2 + 3 };
  122.     /*
  123.      * We use our own patterns since the globals for quickdraw are not
  124.      * directly available in a DA.  (without using a hardcoded address
  125.      * for them and working with offsets.  I like to avoid that when
  126.      * possible.)
  127.      */
  128.     Pattern    myGray = { 170, 85, 170, 85, 170, 85, 170, 85 };
  129.     Pattern    myBlack = { 255, 255, 255, 255, 255, 255, 255, 255 };
  130.  
  131.     TextFont(courier);
  132.     TextSize(12);
  133.     TextFace(bold);
  134.  
  135.     for (i = 0, c = "high byte"; *c; c++, i++) {
  136.         MoveTo(4, i * 12 + (bounds.bottom + bounds.top) / 2 - 54);
  137.         DrawChar(*c);
  138.     }
  139.     CenterDrawPStr("\plow byte ", bounds.left, bounds.right, 10);
  140.     PenPat(myGray);
  141.     for (i = 0; i < 16; i++) {
  142.         ch = i + ((i < 10) ? '0' : 'A' - 10);
  143.         MoveTo(ASCII_H_EDGE + i * ASCII_H_SPC, ASCII_V_EDGE - 14);
  144.         DrawChar(ch);
  145.         MoveTo(ASCII_H_EDGE - 14, ASCII_V_EDGE + i * ASCII_V_SPC);
  146.         DrawChar(ch);
  147.     }
  148.     for (i = 1; i < 16; i++) {
  149.         MoveTo(bounds.left + i * ASCII_H_SPC, bounds.top);
  150.         Line(0, bounds.bottom - bounds.top - 1);
  151.         MoveTo(bounds.left, bounds.top + i * ASCII_V_SPC);
  152.         Line(bounds.right - bounds.left - 1, 0);
  153.     }
  154.     PenPat(myBlack);
  155.     FrameRect(&bounds);
  156.     TextFace(0);
  157.     for (v = 0; v < 16; v++) {
  158.         for (h = 0; h < 16; h++) {
  159.             ch = v * 16 + h;
  160.             if (ch < 32) {
  161.                 TextSize(9);
  162.                 MoveDrawPStr(lowAscii[ch], 
  163.                          ASCII_H_EDGE + h * ASCII_H_SPC - 4,
  164.                          ASCII_V_EDGE + v * ASCII_V_SPC - 1);
  165.             } else {
  166.                 TextSize(12);
  167.                 MoveTo(ASCII_H_EDGE + h * ASCII_H_SPC,
  168.                        ASCII_V_EDGE + v * ASCII_V_SPC);
  169.                 DrawChar(ch);
  170.             }
  171.         }
  172.     }
  173. }
  174.  
  175.  
  176.